﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Rob Ashton 2009
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@mvcex.com
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Web;

namespace MvcEx.Views
{
    public class MvcExViewEngine : IViewEngine
    {
        private IMvcExViewEngineConfigurationSelector mConfigProvider;

        public MvcExViewEngine(IMvcExViewEngineConfigurationSelector configurationProvider)
        {
            mConfigProvider = configurationProvider;
        }

        #region IViewEngine Members

        public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            String controller = (String)controllerContext.RouteData.Values["controller"];
            String filename = partialViewName + ".ascx";

            String location = LocateFile(controllerContext, filename,controller);
            if (string.IsNullOrEmpty(location))
            {
                location = LocateFile(controllerContext, filename, "shared");
            }

            return new ViewEngineResult(
                new WebFormView(location),
                this
            );
        }

        public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return new ViewEngineResult(
                new WebFormView(
                    LocateFile(controllerContext, viewName + ".aspx", (String)controllerContext.RouteData.Values["controller"]),
                    masterName),
                this);
        }

        private String LocateFile(ControllerContext controllerContext, String fileName, String folder)
        {
            MvcExViewEngineConfiguration config = mConfigProvider.GetConfiguration();
            foreach (var component in config.Components.Reverse())
            {
                String componentFolder = Path.Combine(config.Root, component);
                String searchFolder = Path.Combine(componentFolder, "views");
                searchFolder = Path.Combine(searchFolder, folder);
                String searchFile = Path.Combine(searchFolder, fileName);

                if (File.Exists(controllerContext.HttpContext.Server.MapPath(searchFile))) { return searchFile; }
            }
            return null;
        }

        public void ReleaseView(ControllerContext controllerContext, IView view)
        {
            // Nada

        }

        #endregion
    }
}
